package com.jinke.api.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.common.exception.Asserts;
import com.jinke.api.modules.app.controller.res.PriceRuleImportDto;
import com.jinke.api.modules.app.controller.res.RuleSurchargeImportDto;
import com.jinke.api.modules.app.model.PriceRule;
import com.jinke.api.modules.app.service.PriceRuleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 自定义价格表 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-07-17
 */
@RestController
@RequestMapping("/app/priceRule")
@RequiredArgsConstructor
public class PriceRuleController {

    private final PriceRuleService priceRuleService;

    /**
     * @Author: hhp
     * @Description: 承运商根据serviceCode获取价格列表
     * @Data: 2024/7/18 11:00
     * @param  serviceCode 货运服务分组
     * @return: com.jinke.api.common.api.CommonResult<java.util.List<com.jinke.api.modules.app.model.PriceRule>>
     **/
    @ApiOperation("获取所有价格列表")
    @PostMapping("/list")
    public CommonResult<List<PriceRule>> getPriceByServiceCode(@ApiParam("渠道id") @RequestParam String serviceCode) {

        return CommonResult.success(priceRuleService.list(new LambdaQueryWrapper<PriceRule>()
                .eq(PriceRule::getServiceCode, serviceCode)
                .select(PriceRule::getId, PriceRule::getPoundsLine, PriceRule::getOzLine,
                        PriceRule::getZone1, PriceRule::getZone2, PriceRule::getZone3, PriceRule::getZone4,
                        PriceRule::getZone5, PriceRule::getZone6, PriceRule::getZone7, PriceRule::getZone8, PriceRule::getZone9)
                .orderByDesc(PriceRule::getCreatedAt)));
    }

    @ApiOperation("导入自定义价格表")
    @PostMapping("/importPrice")
    public CommonResult<String> importPriceRule(PriceRuleImportDto importDto) throws IOException {
        return priceRuleService.importPriceRules(importDto);
    }

    @ApiOperation("导入价格表和附加费表")
    @PostMapping("/importRuleSurcharge")
    public CommonResult<String> importRuleSurcharge(RuleSurchargeImportDto importDto) throws IOException {
        return priceRuleService.importRuleSurcharge(importDto);
    }
}

